implement igtut04;

include "sys.m";
    sys: Sys;

include "draw.m";
    draw: Draw;
    Point, Rect, Pointer, Image, Display, Font, Screen: import draw;

include "tk.m";
include "wmclient.m";
    wmclient: Wmclient;
    Window: import wmclient;
   
KEYBD_UP:    con 57362;
KEYBD_DOWN:  con 57363;
KEYBD_LEFT:  con 57364;
KEYBD_RIGHT: con 57365;

igtut04: module
{
    init: fn(ctxt: ref Draw->Context, args: list of string);
};

init(ctxt: ref Draw->Context, args: list of string)
{
    running: int = 0;
    
    sys = load Sys Sys->PATH;
    draw = load Draw Draw->PATH;
    wmclient = load Wmclient Wmclient->PATH;
    
    # Lets run this program in a new process group so we don't muck with the
    # program that started it.
    sys->pctl(Sys->NEWPGRP, nil);
    
    wmclient->init();
    
    if (ctxt == nil)
        ctxt = wmclient->makedrawcontext();
        
    buts := wmclient->Resize | 
            wmclient->Hide | 
            wmclient->Help | 
            wmclient->OK;
            
    window := wmclient->window(ctxt, "Inferno Graphics Tutorial #4", buts);
    
    winsz: Rect;
    winsz.min.x = 0;
    winsz.min.y = 0;
    winsz.max.x = 320;
    winsz.max.y = 240;
    
    window.reshape(winsz);
    window.startinput("kbd" :: "ptr" :: nil);
    
    window.onscreen("place");
    
    drawstuff(window);
    
    for(;;) {
        alt {
            # Process Window Manager and Window Messages
            ctl := <-window.ctl or ctl = <-window.ctxt.ctl =>
                window.wmctl(ctl);
                if(ctl != nil && ctl[0] == '!')
                    drawstuff( window );
                if(ctl != nil && ctl == "help")
                    sys->print("Help Pressed.\n");
                if(ctl != nil && ctl == "ok")
                    sys->print("OK Pressed.\n");
                if(ctl != nil && ctl == "exit")
                    sys->print("Closing app.\n");
                    break;
            # Process Keyboard Events
            kbd := <-window.ctxt.kbd =>
                prockeybd(kbd);
            # Process Mouse Pointer Messages
            p := <-window.ctxt.ptr =>
                window.pointer(*p);
                procptr( window, p );
        }
    }
    
    sys->print("Exiting.\n");
}

prockeybd(keycode: int)
{
    if (keycode == KEYBD_UP)
    {          
        sys->print("Up Arrow Key Pressed.\n");
    }
    if (keycode == KEYBD_DOWN)
    {
        sys->print("Down Arrow Key Pressed.\n");
    }
    if (keycode == KEYBD_LEFT)
    {
        sys->print("Left Arrow Key Pressed.\n");              
    }
    if (keycode == KEYBD_RIGHT)
    {
        sys->print("Right Arrow Key Pressed.\n");
    }    
}

procptr(win: ref Window, pt: ref Pointer)
{
    if (win.image.r.contains( pt.xy ))
        sys->print("Button: %d X: %d Y: %d\n", pt.buttons, pt.xy.x, pt.xy.y);
}

drawstuff(win: ref Window)
{
    display := win.display;
    image := win.image;
    
    red := display.color(Draw->Red);
    image.draw(image.r, red, nil, image.r.min);
}